package io.openmessaging.storage.dledger.protocol;

//客户端发送日志的请求协议字段
public class RequestOrResponse {

    //Raft 复制组所属组名
    protected String group;

    //请求目的节点ID
    protected String remoteId;

    //发起请求节点ID
    protected String localId;

    //请求响应字段，表示返回响应码
    protected int code = DLedgerResponseCode.SUCCESS.getCode();

    //请求响应字段，表示返回响应码
    protected String leaderId = null;

    //集群当前选举轮次
    protected long term = -1;

    @Override
    public String toString() {
        return "RequestOrResponse{" +
                "group='" + group + '\'' +
                ", remoteId='" + remoteId + '\'' +
                ", localId='" + localId + '\'' +
                ", code=" + code +
                ", leaderId='" + leaderId + '\'' +
                ", term=" + term +
                '}';
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public RequestOrResponse code(int code) {
        this.code = code;
        return this;
    }

    public void setIds(String localId, String remoteId, String leaderId) {
        this.localId = localId;
        this.remoteId = remoteId;
        this.leaderId = leaderId;
    }

    public String getRemoteId() {
        return remoteId;
    }

    public void setRemoteId(String remoteId) {
        this.remoteId = remoteId;
    }

    public String getLocalId() {
        return localId;
    }

    public void setLocalId(String localId) {
        this.localId = localId;
    }

    public String getLeaderId() {
        return leaderId;
    }

    public void setLeaderId(String leaderId) {
        this.leaderId = leaderId;
    }

    public long getTerm() {
        return term;
    }

    public void setTerm(long term) {
        this.term = term;
    }

    public RequestOrResponse copyBaseInfo(RequestOrResponse other) {
        this.group = other.group;
        this.term = other.term;
        this.code = other.code;
        this.localId = other.localId;
        this.remoteId = other.remoteId;
        this.leaderId = other.leaderId;
        return this;
    }

    public String baseInfo() {
        return String.format("info[group=%s,term=%d,code=%d,local=%s,remote=%s,leader=%s]", group, term, code, localId, remoteId, leaderId);
    }

}
